package com.sczmgk.hotline.popup;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sczmgk.hotline.R;
import com.sczmgk.hotline.utils.CommonUtils;
import com.sczmgk.hotline.utils.MyLog;
import com.sczmgk.hotline.utils.UiUtilities;
import com.sczmgk.hotline.utils.XmlParser;

/**
 * 
 *自定义 popupWindow
 */
public class PupDialog extends PopupWindow implements OnClickListener {
    public static final String ID_PUBLIC_SHARE = "2";
    public static final String ID_CLEAR_CHAT_CONTONT = "4";
    public static final String ID_SET_BG = "5";
    /**
     * 选择图片pup的ID
     */
    public static final String ID_PHOTO = "6";
    public static final String ID_PHOTOGRAPH = "7";
    public static final String ID_SEX = "8";
    public static final String ID_SAVE_BARCODE = "9";
    public static final String ID_FRIENDS = "12";
    public static final String ID_BLACK_LIST = "13";
    public static final String ID_SHOW_PHONE = "15";
    public static final String ID_LINK_SHARE = "18";
    public static final String ID_SHOW_PHONE_CONTACT_SELET = "19";
    public static final String ID_MATCH_PHONE = "21";
    public static final String ID_NEARBY = "22";
    public static final String ID_CLEAN_ALL="23";
    public static final String ID_MY_COUPON="24";
    public static final String ID_PERSONAL_CLEAR_CACHE="25";
    public static final String ID_MY_COLLECTION="26";
    public static final String ID_MY_LOGIN_OUT="27";


    private static final String TAG = "PupDialog";
    private boolean isChangeColor;

    /**
     *  事件的处理（点击回调接口）
     */
    private PupEvent mEvent;
   /**
    *  匹配的字符串ID
    */
    private String popupWindowid;

    /**
     * 根据上下文创建dialog
     * 
     * @param context 要创建dialog的上下文
     * @param pupid 弹出框ID
     * @param popEvent 点击事件回调接口
     */
    public PupDialog(Context context, String pupid, PupEvent popEvent) {
        this(context, pupid, popEvent, null);
    }

    /**
     * 根据上下文创建dialog
     *
     * @param context 要创建dialog的上下文
     * @param pupid 弹出框ID
     * @param popEvent 点击事件回调接口
     * @param message
     */
    public PupDialog(Context context, String pupid, PupEvent popEvent, String message) {
        super(context);
        mEvent = popEvent;
        popupWindowid = pupid;
        String content = XmlParser.getPupWindow(context, pupid);//取得相应id的内容如：拍照/从相册选择图片
        if (!CommonUtils.StringIsNull(content)) {
            init(content.split("/"), context, popEvent, message);
        }
    }

    /**
     * 根据上下文创建dialog
     *
     * @param context 要创建dialog的上下文
     * @param pupid 弹出框ID
     * @param popEvent 点击事件回调接口
     * @param message
     */
    public PupDialog(Context context, String pupid, PupEvent popEvent, String message , boolean isChangeColor) {
        super(context);
        mEvent = popEvent;
        popupWindowid = pupid;
        this.isChangeColor = isChangeColor ;
        String content = XmlParser.getPupWindow(context, pupid);//取得相应id的内容如：拍照/从相册选择图片
        if (!CommonUtils.StringIsNull(content)) {
            init(content.split("/"), context, popEvent, message);
        }
    }

    /**
     * 初始化弹出框体
     * @param curWondowInfo  pup条目的数组如：["拍照","从手机相册选择"]
     * @param context
     * @param pupEvent  点击事件的回调接口
     * @param message
     */
    private void init(String[] curWondowInfo, Context context, PupEvent pupEvent, String message) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout windowView = (LinearLayout) inflater.inflate(R.layout.cpopupwindow_layout, null);
        final LinearLayout lin = (LinearLayout) windowView.findViewById(R.id.pop_layout);
        for (int i = 0; i < curWondowInfo.length; i++) {//根据xml文件中字符串的数量，实例化条目
            lin.addView(createBotton(context, lin, curWondowInfo[i], i));//实例按钮，并设置点击监听（设置TAG（TAG为index,便于回调点击事件））
        }
        lin.addView(createCancelBotton(context, lin, "取消", curWondowInfo.length));

        // 显示附加消息
        if (!TextUtils.isEmpty(message)) {
            TextView messageView = (TextView) windowView.findViewById(R.id.pop_message);
            messageView.setText(message);
            UiUtilities.setVisibilitySafe(messageView, View.VISIBLE);
        }
        // 设置SelectPicPopupWindow的View
        this.setContentView(windowView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x55000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        windowView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = lin.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        ColorDrawable ssdw = new ColorDrawable(0xb0000000);
                        PupDialog.this.setBackgroundDrawable(ssdw);
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
	/**
	 * 创建popupWindow的条目Button,并设置点击事件
	 * @param context
	 * @param root
	 * @param text  按钮的文字
	 * @param index  按钮的索引
	 * @return  返回按钮
	 */
    private View createBotton(Context context, ViewGroup root, String text, int index) {
        LayoutInflater factory = LayoutInflater.from(context);
        LinearLayout conrtainer = (LinearLayout) factory.inflate(R.layout.pup_menu_button, root, false);
        Button bt = (Button) conrtainer.findViewById(R.id.bt_item);
        bt.setText(text);
        conrtainer.setTag(index);
        bt.setOnClickListener(this);
        bt.setTag(index);//给按钮设置标签
        if (index == 0){
            if (isChangeColor){
                bt.setTextColor(context.getResources().getColor(R.color.main_home_search_bar_text_color));
                bt.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
            }
        }
//        conrtainer.setOnClickListener(this);
        return conrtainer;
    }

    /**
     * 创建取消按钮
     * @param context
     * @param root
     * @param text  按钮文字
     * @param index  按钮的索引
     * @return   返回取消按钮
     */
	private View createCancelBotton(Context context, ViewGroup root, String text, int index) {
        LayoutInflater factory = LayoutInflater.from(context);
        LinearLayout conrtainer = (LinearLayout) factory.inflate(R.layout.pup_cancel_button, root, false);
        Button bt = (Button) conrtainer.findViewById(R.id.bt_item);
        bt.setText(text);
        bt.setOnClickListener(this);
        bt.setTag(index);
        return conrtainer;
    }
	/**
	 * 回调点击事件
	 */
    @Override
    public void onClick(View item) {
    	MyLog.d(TAG,"onclick　："+item.getTag());
        int tag = (Integer) item.getTag();
        mEvent.itemOnClick(PupDialog.this, item, tag);

    }

    /**
     * 取得popupWindow的Id
     * @return
     */
    public String getId() {
        return popupWindowid;
    }
    /**
     * popupWindow  按钮点击事件的接口
     * @author
     *
     */
    public static interface PupEvent {
        // <pup id="2"> 推荐给朋友/清空内容 </pup>
        int SHARE_FRIEND_CIRCLE = 0;
        int CLEAN_CONTENT = 1;

        // <pup id="5"> 清空聊天记录 </pup>
        int CLEAR_CHAT = 0;
        // <pup id="5"> 聊天背景/拍照/从手机相册选择 </pup>
        int SET_BG_CHOOSE = 0;
        int SET_BG_CAPTURE = 1;
        int SET_BG_CHOOSE_GALLERY = 2;

        // 点对点聊天照片
        // <pup id="6"> 拍照/从手机相册选择</pup>
        int PHOTO_CAPTURE = 0;
        int PHOTO_GALLERY = 1;

        // <pup id="7"> 拍摄视频/从手机相册选择</pup>
        int PHOTOGRAPH_REC = 0;
        int PHOTOGRAPH_GALLERY = 1;

        // <pup id="7"> 拍摄视频/从手机相册选择</pup>
        int SEX_MAN = 0;
        int SEX_WOMAN = 1;
        int SEX_NA = 2;

        // <pup id="9">保存二维码到相册</pup>
        int SAVE_BARCODE_TO_GALLERY = 0;

        // <pup id="12">发送该名片/备注名/加入黑名单/删除好友</pup>
        int FRIENDS_SEND_VCARD = 0;
        int FRIENDS_REMARK_NAME = 1;
        int FRIENDS_ADD_BLACK_LIST = 2;
        int FRIENDS_DELETE_FRIEND = 3;

        // <pup id="13"> 发送该名片/备注名/移出黑名单/删除好友 </pup>
        int BLACK_LIST_SEND_VCARD = 0;
        int BLACK_LIST_REMARK_NAME = 1;
        int BLACK_LIST_REMOVE_BLACK_LIST = 2;
        int BLACK_LIST_DELETE_FRIEND = 3;

        // <pup id="15">发起呼叫 </pup>
        int SHOW_PHONE_DIAL = 0;

        // <pup id="17">删除回复 </pup>
        int DELETE_REPLY = 0;

        // <pup id="18">复制链接/分享到朋友圈/在浏览器中打开</pup>
        int LINK_CONTENT_COPY = 0;
        int LINK_SHARE_FRIENDCIRCLE = 1;
        int LINK_BROWSER_OPEN = 2;
        // <pup id="20">
        int CONTENT_COPY = 0;

        // <pup id="21">
        int MATCH_PHONE_TEL = 0;
        int MATCH_PHONE_CREATE = 1;
        int MATCH_PHONE_COPY = 2;
        
        // <pup id="22">
        int NEARBY_CLEAN = 0;
        int NEARBY_CANCEL = 1;
        
        // <pup id="23">
        int CLEAN_ALL=0;

        /**
         * popupWindow条目点击的回调
         * @param pupid  popupwindowID（如：选择相片）
         * @param item
         * @param position   点击的位置，如：0，照相， 1.选择相册
         */
        void itemOnClick(PupDialog pupid, View item, int position);
    }
}
